package com.bolingcavalry.crm.phone_record;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bolingcavalry.crm.phone_record.mapper.PhoneRecordMapper;
import com.bolingcavalry.crm.phone_record.service.PhoneRecordService;
import com.bolingcavalry.crm.security.utils.SecurityUtils;
import com.bolingcavalry.crm.utils.AUtils;
import com.entity.phone_records.PhoneRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 电话记录表 服务实现类
 */
@Service
public class PhoneRecordServiceImpl extends ServiceImpl<PhoneRecordMapper, PhoneRecord> implements PhoneRecordService {
    @Autowired
    private AUtils aUtils;

    @Override
    public IPage<PhoneRecord> findPhoneRecords(Integer current, Integer size, String createBy,String customerCompany, String status) {
        // 创建分页对象
        Page<PhoneRecord> page = new Page<>(current, size);
        // 构建查询条件
        LambdaQueryWrapper<PhoneRecord> queryWrapper = new LambdaQueryWrapper<>();
        //获取当前登录用户的角色信息
        for (String currentUserRole : SecurityUtils.getCurrentUserRoles()) {
            //判断是否存在ADMIN角色
            if ("ADMIN".equals(currentUserRole)) {
                //ADMIN角色，则查询所有记录

            }
            //否则需要加一个条件判断创建人是否为当前登陆人
            else{
                queryWrapper.eq(PhoneRecord::getCreateBy, aUtils.getNickNameByuserName(SecurityUtils.getCurrentUsername()));
            }
        }
        if (StringUtils.hasText(createBy)) {
            queryWrapper.like(PhoneRecord::getCreateBy, createBy); // 使用 like 模糊匹配
        }
        if(StringUtils.hasText(customerCompany)){
            queryWrapper.like(PhoneRecord::getCustomerCompany, customerCompany);
        }
        if(StringUtils.hasText(status)){
            queryWrapper.eq(PhoneRecord::getStatus, status);
        }
        // 执行分页查询
        return this.page(page, queryWrapper);
    }


}